﻿# ------------------------------------ #
# Office Web Apps Server Configuration #
# ------------------------------------ #
param([string]$virtualMachineService, [string]$virtualMachineName)

Write-Information "Virtual machine '$virtualMachineName' Office Web Apps Server '$($script.type)' configuration ... " -NoNewLine $true
$userName = $subscription.virtualMachines.adminUserName
$password = $subscription.virtualMachines.password
$netBiosDomain = $subscription.virtualMachines.netBiosDomain
$uri = Get-AzureWinRMUri -ServiceName $virtualMachineService -Name $virtualMachineName
$credential = New-Object System.Management.Automation.PSCredential(($netBiosDomain + "\" + $userName), (ConvertTo-SecureString $password -AsPlainText -Force))
$session = New-PSSession -ComputerName $uri[0].DnsSafeHost -Credential $credential -Port (Get-VirtualMachineInstanceEndpointPort $subscription $virtualMachineName) -UseSSL -Authentication Credssp
switch ($script.type)
{
	"Prepare"
	{
		Invoke-Command -Session $session -ScriptBlock {
			# Install prerequisites
			Import-Module ServerManager
			$result = Add-WindowsFeature Web-Server,Web-Mgmt-Tools,Web-Mgmt-Console,Web-WebServer,Web-Common-Http,Web-Default-Doc,Web-Static-Content,Web-Performance,Web-Stat-Compression,Web-Dyn-Compression,Web-Security,Web-Filtering,Web-Windows-Auth,Web-App-Dev,Web-Net-Ext45,Web-Asp-Net45,Web-ISAPI-Ext,Web-ISAPI-Filter,Web-Includes,InkandHandwritingServices -Source W:\sources\sxs -WarningAction Ignore
			if (!$? -or $result.Success -eq $false) {throw ".NET Framework 3.5 deployment failed."}
		}
        Remove-PSSession $session
        Write-Text "done."
        Restart-VirtualMachineInstance $subscription $virtualMachineService $virtualMachineName $true
        return
	}
	"Install"
	{
		Invoke-Command -Session $session -ScriptBlock {
			if ((Start-Process -FilePath "S:\setup.exe" -ArgumentList "/config S:\files\setupsilent\config.xml" -Wait -PassThru).ExitCode -ne 0)
            {
                throw "Office Web Apps Server installation failed. Check '$Env:TEMP\Wac Server Setup(*).log' for more details."
            }
		}
	}
	"Create"
	{
		Invoke-Command -Session $session -ScriptBlock {
			param([string]$internalUrl, [string]$externalUrl, [string]$hostDomain, [string]$certificateName)
			# Create farm
			Import-Module OfficeWebApps
			$result = New-OfficeWebAppsFarm -InternalUrl $internalUrl -ExternalUrl $externalUrl -CertificateName $certificateName -ClipartEnabled -TranslationEnabled -OpenFromUrlEnabled -OpenFromUncEnabled -EditingEnabled -Force -Confirm:$false -WarningAction Ignore
			# Add new host
			$result = New-OfficeWebAppsHost –Domain $hostDomain
            if ((Get-OfficeWebAppsFarm -ErrorAction SilentlyContinue) -eq $null)
            {
                throw "Office Web Apps farm configuration failed."
            }
            # Validate deployment at: https://[FQDN]/hosting/discovery, https://[FQDN]/op/generate.aspx
		} -ArgumentList $script.internalUrl, $script.externalUrl, $script.host, $script.certificateName
	}
	"Join"
	{
		Invoke-Command -Session $session -ScriptBlock {
			param([string]$primary)
			# Join farm
			Import-Module OfficeWebApps
			$result = New-OfficeWebAppsMachine –MachineToJoin $primary
		} -ArgumentList $script.primary
	}
	"Connect"
	{
		Invoke-Command -Session $session -ScriptBlock {
			param([string]$zone, [string]$serverName)
			# Connect to farm
			Add-PSSnapin Microsoft.SharePoint.PowerShell
			Remove-SPWOPIBinding -All -Confirm:$false
			Set-SPWOPIZone -Zone $zone | Out-Null
			New-SPWOPIBinding -ServerName $serverName | Out-Null
		} -ArgumentList $script.zone, $script.serverName
	}
	default
	{
		throw "Office Web Apps Server type '$($script.type)' is not supported."
	}
}
Remove-PSSession $session
Write-Text "done."